# -*- coding: utf-8 -*-

"""
* Copyright (C) 2016 GridSafe, Inc.
"""
import pytz
from datetime import datetime
from django.conf import settings
from rest_framework import serializers
from django.contrib.auth.models import User

from apps.wallet.models.profile import Profile
from easy_thumbnails.fields import ThumbnailerImageField


class TimestampFiled(serializers.Field):
    def to_internal_value(self, data):
        current_timezone = pytz.timezone(settings.TIME_ZONE)
        return datetime.fromtimestamp(float(data), current_timezone)

    def to_representation(self, value):
        return str(int(value.timestamp()))


class ProfileSerializer(serializers.ModelSerializer):
    class Meta:
        model = Profile

    def __init__(self, *args, **kwargs):
        self.size_alias = kwargs.pop('size_alias', 'large')
        kwargs['read_only'] = True
        super(ThumbnailerImageField, self).__init__(*args, **kwargs)

    def to_representation(self, value):
        try:
            return value[self.size_alias].url
        except Exception:
            return None

    fields = {'user_id', 'username', 'phone', 'sex', 'carrer', 'photo', 'description'}

class UserSerializer(serializers.ModelSerializer):

    class Meta:
        model = User


